package bsc.war.ajax;



import bsc.ejb.entity.ColorimetrySettings;
import bsc.ejb.entity.Person;
import bsc.ejb.entity.Project;
import bsc.ejb.entity.Task;
import bsc.ejb.entity.Tracer;
import bsc.ejb.metadata.Colorimetry;
import bsc.ejb.metadata.Framework;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.BSCUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author memozac
 */
public class DesignerAddProject extends AbstractController  {

     @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
         HttpServletResponse response) throws Exception {

        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            //Long idScoreCard = request.getParameter("scoreCard") != null && NumberUtils.isNumber(request.getParameter("scoreCard")) ? Long.parseLong(request.getParameter("scoreCard")) : null;
            //ScoreCard scoreCard = delegate.findScoreCard(idScoreCard);
            List<Person> persons = delegate.findAvaliablePerson();
            //List<Dimension> dimensions = delegate.findDimension();
            Framework framework = delegate.findFramework(Boolean.TRUE);
            List<Colorimetry> colorimetries = delegate.findColorimetry(framework, Boolean.TRUE);

            String type = request.getParameter("type");
            Project project = "edit".equals(type) && NumberUtils.isDigits(request.getParameter("id")) ? delegate.findProject(Long.valueOf(request.getParameter("id"))) : null;

            Map<Colorimetry, Double> minColorValue = new HashMap<Colorimetry, Double>();
            Map<Colorimetry, Double> maxColorValue = new HashMap<Colorimetry, Double>();
            Tracer budget = null, progress = null;
            if(project != null){
                Map<Task, List<Task>> tasks = new HashMap<Task, List<Task>>();
                Task rootTask = delegate.findRootTask(project);
                BSCUtils.getTaskMap(tasks, rootTask, delegate);
                request.setAttribute("rootTask", rootTask);
                request.setAttribute("tasks", tasks);

                // if(!persons.contains(project.getResponsible())) persons.add(project.getResponsible());

                List<ColorimetrySettings> settings = delegate.findColorimetrySettings(project);
                for( ColorimetrySettings setting : settings ) {
                    minColorValue.put(setting.getColorimetry(), setting.getMinScore()*100);
                    maxColorValue.put(setting.getColorimetry(), setting.getMaxScore()*100);
                }

                budget = delegate.findTracer(project, Tracer.Type.BUDGET);
                progress = delegate.findTracer(project, Tracer.Type.PROGRESS);
            } else {
                for(Colorimetry colorimetry : colorimetries) {
                    minColorValue.put(colorimetry, null);
                    maxColorValue.put(colorimetry, null);
                }
            }

            request.setAttribute("project", project);
            //request.setAttribute("scoreCard", scoreCard);
            request.setAttribute("persons", persons);
            //request.setAttribute("dimensions", dimensions);
            request.setAttribute("colorimetries", colorimetries);
            request.setAttribute("minColorValue",minColorValue);
            request.setAttribute("maxColorValue",maxColorValue);
            request.setAttribute("budget", budget);
            request.setAttribute("progress", progress);
        } catch (Exception e) {
            //System.err.println(e);
            e.printStackTrace();
        }

         return new ModelAndView("designerAddProject");
    }

}